package com.liushao.controller;


import com.liushao.hfb.RequestHelper;
import com.liushao.result.Result;
import com.liushao.service.UserAccountService;
import com.liushao.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/api/core/userAccount")
public class UserAccountController {

    @Autowired
    private UserAccountService userAccountService;

    /*
         .$post(
                  '/api/core/userAccount/auth/commitCharge/' + _this.chargeAmt
                )
                .then((response) => {
                  document.write(response.data.formStr)
     */

    @PostMapping("/auth/commitCharge/{chargeAmt}")
    public Result commitCharge(
            @PathVariable BigDecimal chargeAmt, HttpServletRequest request
            ){
        //token中拿userId
        
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }
        Long userId = JwtUtils.getUserId(token);

        String formStr= userAccountService.commitCharge(chargeAmt,userId);

        return  Result.ok().data("formStr",formStr);

    }


    // /api/core/userAccount/notify 充值回调    幂等性

    @PostMapping("/notify")
    public  String notifyss(HttpServletRequest request){
        Map<String, Object> objectMap = RequestHelper.switchMap(request.getParameterMap());
        if(RequestHelper.isSignEquals(objectMap)){
              //充值成功 ???  0001=充值成功
            if("0001".equals(objectMap.get("resultCode"))){
                    userAccountService.notifyss(objectMap);
                    return "success";
            }else{
                 return "fail";
            }
        }
           return "fail";
    }


    //  /api/core/userAccount/auth/getAccount

    /**
     * 查询账户余额
     * @param request
     * @return
     */
    @GetMapping("/auth/getAccount")
    public Result getAccount(HttpServletRequest request){
        
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }
        Long userId = JwtUtils.getUserId(token);

        BigDecimal money=userAccountService.getAccountById(userId);

        return Result.ok().data("account",money);


    }

    //  '/api/core/userAccount/auth/commitWithdraw/' + this.fetchAmt

    @ApiOperation("提现")
    @PostMapping("/auth/commitWithdraw/{fetchAmt}")
    public Result commitWithdraw(
            @ApiParam(value = "金额",required = true)
            @PathVariable BigDecimal fetchAmt,
            HttpServletRequest request
    ){
        
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }
        Long userId = JwtUtils.getUserId(token);

        //动态表单
        String formStr = userAccountService.commitWithdraw(fetchAmt,userId);

        return Result.ok().data("formStr",formStr);


    }


    //http://localhost/api/core/userAccount/notifyWithdraw";

    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request){
        Map<String, Object> objectMap = RequestHelper.switchMap(request.getParameterMap());

        if(RequestHelper.isSignEquals(objectMap)){
              if("0001".equals(objectMap.get("resultCode"))){
                        //提现成功了
                  userAccountService.notifysssss(objectMap);
              }else{
                  return "fail";
              }
        }else{
            System.out.println("签名错误 ");
            return "fail";
        }

        return  "success";
    }



}

